[...path].ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import type { APIEvent } from "@solidjs/start/server"
  2. import { Hono } from "hono"
  3. import { describeRoute, openAPIRouteHandler, resolver } from "hono-openapi"
  4. import { validator } from "hono-openapi"
  5. import z from "zod"
  6. import { cors } from "hono/cors"
  7. import { Share } from "~/core/share"
  8. const app = new Hono()
  9. app
  10. .basePath("/api")
  11. .use(cors())
  12. .get(
  13. "/doc",
  14. openAPIRouteHandler(app, {
  15. documentation: {
  16. info: {
  17. title: "Opencode Enterprise API",
  18. version: "1.0.0",
  19. description: "Opencode Enterprise API endpoints",
  20. },
  21. openapi: "3.1.1",
  22. },
  23. }),
  24. )
  25. .post(
  26. "/share",
  27. describeRoute({
  28. description: "Create a share",
  29. operationId: "share.create",
  30. responses: {
  31. 200: {
  32. description: "Success",
  33. content: {
  34. "application/json": {
  35. schema: resolver(
  36. z
  37. .object({
  38. id: z.string(),
  39. url: z.string(),
  40. secret: z.string(),
  41. })
  42. .meta({ ref: "Share" }),
  43. ),
  44. },
  45. },
  46. },
  47. },
  48. }),
  49. validator("json", z.object({ sessionID: z.string() })),
  50. async (c) => {
  51. const body = c.req.valid("json")
  52. const share = await Share.create({ sessionID: body.sessionID })
  53. const protocol = c.req.header("x-forwarded-proto") ?? c.req.header("x-forwarded-protocol") ?? "https"
  54. const host = c.req.header("x-forwarded-host") ?? c.req.header("host")
  55. return c.json({
  56. id: share.id,
  57. secret: share.secret,
  58. url: `${protocol}://${host}/share/${share.id}`,
  59. })
  60. },
  61. )
  62. .post(
  63. "/share/:shareID/sync",
  64. describeRoute({
  65. description: "Sync share data",
  66. operationId: "share.sync",
  67. responses: {
  68. 200: {
  69. description: "Success",
  70. content: {
  71. "application/json": {
  72. schema: resolver(z.object({})),
  73. },
  74. },
  75. },
  76. },
  77. }),
  78. validator("param", z.object({ shareID: z.string() })),
  79. validator("json", z.object({ secret: z.string(), data: Share.Data.array() })),
  80. async (c) => {
  81. const { shareID } = c.req.valid("param")
  82. const body = c.req.valid("json")
  83. await Share.sync({
  84. share: { id: shareID, secret: body.secret },
  85. data: body.data,
  86. })
  87. return c.json({})
  88. },
  89. )
  90. .get(
  91. "/share/:shareID/data",
  92. describeRoute({
  93. description: "Get share data",
  94. operationId: "share.data",
  95. responses: {
  96. 200: {
  97. description: "Success",
  98. content: {
  99. "application/json": {
  100. schema: resolver(z.array(Share.Data)),
  101. },
  102. },
  103. },
  104. },
  105. }),
  106. validator("param", z.object({ shareID: z.string() })),
  107. async (c) => {
  108. const { shareID } = c.req.valid("param")
  109. return c.json(await Share.data(shareID))
  110. },
  111. )
  112. .delete(
  113. "/share/:shareID",
  114. describeRoute({
  115. description: "Remove a share",
  116. operationId: "share.remove",
  117. responses: {
  118. 200: {
  119. description: "Success",
  120. content: {
  121. "application/json": {
  122. schema: resolver(z.object({})),
  123. },
  124. },
  125. },
  126. },
  127. }),
  128. validator("param", z.object({ shareID: z.string() })),
  129. validator("json", z.object({ secret: z.string() })),
  130. async (c) => {
  131. const { shareID } = c.req.valid("param")
  132. const body = c.req.valid("json")
  133. await Share.remove({ id: shareID, secret: body.secret })
  134. return c.json({})
  135. },
  136. )
  137. export function GET(event: APIEvent) {
  138. return app.fetch(event.request)
  139. }
  140. export function POST(event: APIEvent) {
  141. return app.fetch(event.request)
  142. }
  143. export function PUT(event: APIEvent) {
  144. return app.fetch(event.request)
  145. }
  146. export async function DELETE(event: APIEvent) {
  147. return app.fetch(event.request)
  148. }